package com.jhm.bing_fa_rong_qi;

import java.util.concurrent.CopyOnWriteArrayList;

public class CopyOnWriteArrayListDemo {
    public static void main(String[] args) throws Exception {
        //线程安全的CopyOnWriteArrayList并发集合
        CopyOnWriteArrayList<String> strings = new CopyOnWriteArrayList<>();
        Runnable runnable = () -> {
            for(int i=0;i<1000;i++){
                strings.add("haha");
            }
        };
        for(int j=0;j<100;j++){
            new Thread(runnable).start();
        }
        Thread.sleep(2000);         //注意：这里不能写1000毫秒，因为这100个线程在1s内执行不完，所以会显示的数字不是100000
        System.out.println(strings.size());
    }
}
